<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>AUI快速完成布局</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style>
        #mf-explain {
            text-align: left;
            padding: 20px;
            background: #fff;
            line-height: 25px;
        }

        #mf-explain h3 {
            line-height: 35px;
        }

        #mf-explain p {
            text-indent: 1rem;
        }
    </style>


</head>

<body>
    <div class="content" style="text-align:center">
        <div>
            <img src="./img/mf/mf_header.png" alt="">
        </div>
        <h3 style="text-align:center;margin-top:20px" id="title1"></h3>
        <p style="text-align:center;padding:10px" id="title_er1"></p>
        <!-- <img src="./img/mf/qrcode_1535678081.png" alt="" style="display:inline;"> -->
        <p>专属二维码 </p>
        <div id="qrcode" ><img src="" alt="" style="display:inline" ontouchstart='longpress(this,"fnlong()","fnclick(this)",800);' id="img"></div>
        <p>
            长按图片保存到手机，然后发送给朋友 ~
        </p>
        <div class="aui-row">
            <div class="aui-col-xs-4" style="padding:10px">
                <img src="" alt="" id="img1" onclick="fnOpenWin(this)">
            </div>
            <div class="aui-col-xs-4" style="padding:10px">
                <img src="" alt="" id="img2" onclick="fnOpenWin(this)">
            </div>
            <div class="aui-col-xs-4" style="padding:10px">
                <img src="" alt="" id="img3" onclick="fnOpenWin(this)">
            </div>
        </div>
        <div class="aui-row" style="background:#fff;">
            <div class="aui-col-xs-6" style="padding:10px">
                <h3>累计奖励</h3>
                <p><span style="color:red;font-size:18px;">973562</span>元</p>
            </div>
            <div class="aui-col-xs-6" style="padding:10px">
                <h3>累计人数</h3>
                <p><span style="color:red;font-size:18px;">19286</span>人</p>
            </div>

        </div>
        <p style="padding:10px;">你还没有邀请记录,看看人家→_→</p>
        <div id="list" style="background:#fff;text-align:left;">

        </div>
    </div>
    <div id="member-friend" style="padding:1rem;line-height:1.4rem;font-size:0.8rem;background:#fff"></div>
</body>

<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="aui-row" style="border-bottom:1px solid #ccc;padding:0.6rem">
        <div class="aui-col-xs-6 ">
            <p>昵称：{{= value.nickname}}</p>
            <p>时间：{{= userDate(value.regdate)}}</p>
            <p>赚佣：{{= value.order_count}}</p>
        </div>
        <div class="aui-col-xs-6 ">
            <p>绑定手机：******</p>
            <p>获金钱：{{= value.total_money}}</p>
            <p>获积分：{{= value.total_point}}</p>
        </div>
    </div>
    {{~}}
</script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="./conf/config.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="./js/jquery.qrcode.min.js"></script>
<script type="text/javascript">
    apiready = function() {

        fnGetData1();
        fnGetData();
        initRefresh();


    };
    //系统长按事件；
    function initRefresh() {
        api.setRefreshHeaderInfo({
            visible: true,
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function(ret, err) {
          fnGetData1();
          fnGetData();
        });
    }

    function fnGetData1() {

        api.ajax({
            url: SITE_URL + 'index.php?m=api&c=appnew&a=get_friends_config',
            method: 'get',
        }, function(ret, err) {
            //api.hideProgress();
            api.refreshHeaderLoadDone();
            fnUpdateList1(ret);
        });

    }

    function fnUpdateList1(ret) {
        //alert(ret.data);
        $("#member-friend").html(ret.data);
        $("#member-friend").find("p").css({
            "text-indent": "1.2rem"
        });
    }

    function fnGetData() {
        var userInfo = $api.getStorage('userInfo');
        //获取数据app中存入的数据然后分配到模板中；
        if (userInfo) {
            var random = userInfo.random;
            var userid = userInfo.userid;
        } else {
            var random = "";
            var userid = "";
        }
        api.ajax({
            url: SITE_URL + 'index.php?m=api&c=appnew&a=member_friend',
            method: 'post',
            data: {
                values: {
                    userid: userid,
                    random: random,
                },

            }
        }, function(ret, err) {
          //api.hideProgress();
          //alert(JSON.stringify(ret));
            switch (ret.status) {
                case 0:
                //api.hideProgress();
                    alert(ret.msg);
                    api.openWin({
                        name: 'login',
                        url: './login.html',
                        pageParam: {
                            name: 'test'
                        }
                    });
                    break;
                case 1:
                    //alert(1111);
                    fnUpdateList(ret);
                    break;
                case 2:

                    //alert(1111);
                    alert(ret.msg);
                    api.openWin({
                        name: 'login',
                        url: './login.html',
                        pageParam: {
                            name: 'test'
                        }
                    });

                    //然后关闭当前网页，跳转到另一个网页；
                    break;
                case 5:
                    fnUpdateListDefault();
                    api.refreshHeaderLoadDone(); //下拉刷新隐藏
                    break;
            }

        });

    }

    function fnUpdateList(ret) {

        // jQuery(function() {
        //     // jQuery('#qrcode').qrcode({
        //     //     render: "canvas", //也可以替换为table
        //     //     width: 135,
        //     //     height: 135,
        //     //     text: ret.data.weixin_url
        //     // });
        //     jQuery('#qrcode').qrcode({width:135,height:135,correctLevel:0,text:ret.data.weixin_url});
        // })
//         jQuery('#qrcode').qrcode({
//     render: "canvas", //也可以替换为table
//     width: 135,
//     height: 135,
//     text: ret.data.weixin_url
// });

    $("#qrcode").find("img").attr("src", SITE_URL + "img/" + ret.data.userid + "/ewm"+ret.data.userid+".png");
        $("#title1").html(ret.data.title);
        $("#title1").html(ret.data.title_er);
        $("#img1").attr("src", SITE_URL + "img/" + ret.data.userid + "/hbb.jpg");
        $("#img2").attr("src", SITE_URL + "img/" + ret.data.userid + "/hbc.jpg");
        $("#img3").attr("src", SITE_URL + "img/" + ret.data.userid + "/hbd.jpg");
        $("#money").html(ret.data.money_number);
        $("#person").html(ret.data.person_number);


        var list = $api.byId('list'); //获取节点
        //var str={"array":["banana","apple","orange"]};
        // 1. 编译模板函数
        var tempFn = doT.template($api.byId('template').innerHTML); //找到模板多次使用
        // 2. 多次使用模板函数
        var resultText = tempFn(ret.data.list);
        $api.html(list, "");
        $api.append(list, resultText);

        //$("#mb-log").html();
    }

    function fnOpenWin(that) {
        var src1 = $(that).attr("src");
        api.openWin({
            name: 'img',
            url: './img.html',
            pageParam: {
                src: src1
            }
        });

    }

    //复制粘贴：
    function longpress(el, fn_longpress, fn_click, longpressTime) {
        longpressTime = longpressTime || 800;
        time_longpress = setTimeout(function() {
            time_longpress = 0;
            eval(fn_longpress);
        }, longpressTime);
        el.ontouchmove = function() {
            clearTimeout(time_longpress);
            time_longpress = 0;
        }
        el.ontouchend = function() {
            clearTimeout(time_longpress);
            if (time_longpress != 0) {
                eval(fn_click);
            }
        }
    }

    function fnlong() {

    // var canvas = $('#qrcode').find("canvas").get(0);
    //
    //     try {//解决IE转base64时缓存不足，canvas转blob下载
    //     var blob = canvas.msToBlob();
    //     navigator.msSaveBlob(blob, 'qrcode.jpg');
    // } catch (e) {//如果为其他浏览器，使用base64转码下载
    //     var url = canvas.toDataURL('image/jpeg');
    //     //$("#download").attr('href', url).get(0).click();
    // }
        api.actionSheet({
            title: '保存相片到相册',

            buttons: ['保存']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {

                fnSaveImg();
            }
        });
    }

    function fnclick(el) {
        api.toast({
            msg: '长按保存',
            duration: 2000,
            location: 'middle'
        });
    }

    //长按下载保存
    function fnSaveImg() {
      var src2=$("#img").attr("src");
       //var timestamp = Date.parse(new Date());
       var timestamp=RandomNumBoth(100000,999999);
        // api.showProgress({
        //     title: '努力保存中...',
        //     text: '先喝杯茶...',
        //     modal: false
        // });
      //alert(url);
        api.download({
            url:src2,
            savePath: 'fs://iamge/'+timestamp+'.jpg'
        }, function(ret, err) {
            if (ret) {
                if (ret.state == 1) {
                    api.saveMediaToAlbum({
                        path: 'fs://iamge/'+timestamp+'.jpg'
                    }, function(ret, err) {
                        if (ret && ret.status) {
                            //alert(JSON.stringify(ret));
                            api.hideProgress();
                            api.confirm({
                                title: '查看图片',
                                msg: '图片保存成功',
                                buttons: ['现在查看', '取消']
                            }, function(ret, err) {
                                var index = ret.buttonIndex;
                                if (index == 1) {
                                    api.getPicture({
                                        sourceType: 'album',
                                        encodingType: 'jpg',
                                        mediaValue: 'pic',
                                        destinationType: 'url',
                                        allowEdit: true,
                                        quality: 50,
                                        targetWidth: 100,
                                        targetHeight: 100,
                                        saveToPhotoAlbum: false
                                    }, function(ret, err) {
                                        //chakan不做处理
                                    });
                                }
                            });
                        } else {
                            alert('保存失败');
                        }
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });


    }
    function RandomNumBoth(Min,Max){

          var Range = Max - Min;

          var Rand = Math.random();

          var num = Min + Math.round(Rand * Range); //四舍五入

          return num;

    }
</script>


</html>
